﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ERP.Infratructure.Interface;

namespace ERP.Infratructure.Implement
{
    public class BaseRepository<T> : IBaseRepository<T> where T : class
    {
        MyDbContext DB;

        public BaseRepository(MyDbContext dB)
        {
            DB = dB;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="t">参数</param>
        /// <returns>影响行数</returns>
        public Task<int> AddAsync(T t)
        {
            DB.Set<T>().AddAsync(t);
            return DB.SaveChangesAsync();
        }
        /// <summary>
        ///修改
        /// </summary>
        /// <param name="t">参数</param>
        /// <returns>影响行数</returns>
        public Task<int> UpdateAsync(T t)
        {
            DB.Set<T>().Update(t);
            return DB.SaveChangesAsync();
        }
        /// <summary>
        /// 获取参数
        /// </summary>
        /// <returns>数据</returns>

        public IQueryable<T> GetAll()
        {
            return DB.Set<T>();
        }
        /// <summary>
        /// 单行数据
        /// </summary>
        /// <param name="Id">主键</param>
        /// <returns>对象</returns>
        public async Task<T> GetValue(long Id)
        {
            return await DB.Set<T>().FindAsync(Id);
        }
        /// <summary>
        /// 批量添加实体集合
        /// </summary>
        /// <param name="t">实体集合</param>
        /// <returns>受影响行数</returns>
        public Task<int> CreateRangeAsync(List<T> t)
        {
            DB.Set<T>().AddRangeAsync(t);
            return DB.SaveChangesAsync();
        }
        /// <summary>
        /// 批量修改实体集合
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public Task<int> UpdateRangeAsync(List<T> t)
        {
            DB.Set<T>().UpdateRange(t);
            return DB.SaveChangesAsync();
        }
    }
}
